import clutter
import gobject

class TouchActor(clutter.Actor):
    def __init__(self, simple_cb=None, release_cb=None):
        clutter.Actor.__init__(self)
        self.set_reactive(True)

        self.simple_cb = simple_cb
        self.release_cb = release_cb

        if simple_cb is not None:
            self.connect("button-press-event", self.press)
        if release_cb is not None:
            self.connect("button-release-event", self.release)

    def press(self, source, event):
        self.simple_cb(source)

    def release(self, source, event):
        self.release_cb(source)

class LongTouchActor(clutter.Actor):
    def __init__(self, simple_cb=None, long_cb=None, long_delay_ms=2000, long_msg=None):
        clutter.Actor.__init__(self)
        self.set_reactive(True)
        self.long_delay_ms = long_delay_ms
        self.long_msg = long_msg
        self.was_short = True

        if long_cb is not None:
            self.connect("button-press-event", self.init_click)
            self.long_cb = long_cb
        if simple_cb is not None:
            self.connect("button-release-event", self.simple_click)
            self.simple_cb = simple_cb

    def init_click(self, source, event):
        self.was_long = True
        self.was_short = True
        gobject.timeout_add(self.long_delay_ms, self.long_click, source)
        if self.long_msg is not None:
            self.launchEvent("info", "Hold %s seconds to %s" %(self.long_delay_ms/1000, self.long_msg))

    def simple_click(self, source, event):
        if self.was_short:
            self.was_long = False
            self.simple_cb(source)

    def long_click(self, source): 
        if self.was_long:
            self.was_short = False
            self.long_cb(source)
        return False
